const JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require("mongoose");//引入数据库
const User = mongoose.model("user");
const keys = require("../config/keys");

const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;



module.exports = passport =>{
    passport.use(new JwtStrategy(opts,(jwt_payload, done) =>{
       console.log(jwt_payload)
       User.findById(jwt_payload.id).then(user =>{
        if(user){
            return done(null,user);//成功
        }
        return done(null,false);//失败
       } )
       .catch(err => console.log(err))
    }));

}